/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mygame.Units;

import com.jme3.asset.AssetManager;
import com.jme3.bounding.BoundingSphere;
import com.jme3.bullet.PhysicsSpace;
import com.jme3.bullet.collision.shapes.HullCollisionShape;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Quaternion;
import com.jme3.math.Vector3f;
import com.jme3.scene.Geometry;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import com.jme3.scene.control.BillboardControl;
import com.jme3.scene.shape.Quad;
import com.jme3.util.TangentBinormalGenerator;

/**
 *
 * @author Ricardo
 */
public class Player extends Unit{
    
    RigidBodyControl rigidBody;
    Vector3f location=new Vector3f();
    Quaternion rotation = new Quaternion();
    
    Node Health;
    
    public Player() {
        super("Player", 400, 4, 1, 1);
    }
    
    @Override
    public void build(AssetManager assetManager, PhysicsSpace physicsSpace, Node rootNode, Node shootables) {
        Spatial geom = assetManager.loadModel("Models/boat.j3o");
        geom.setName(name);
        TangentBinormalGenerator.generate(geom);           // for lighting effect
        Material mat_default = new Material(assetManager, "Common/MatDefs/Light/Lighting.j3md");
        mat_default.setTexture("DiffuseMap", assetManager.loadTexture("Textures/ballon.jpg"));
        mat_default.setTexture("NormalMap", assetManager.loadTexture("Textures/ballon_normal.jpg"));
        mat_default.setBoolean("UseMaterialColors",true);
//        mat_default.setBoolean("VertexLighting", true );
        mat_default.setColor("Specular",ColorRGBA.White);
        mat_default.setColor("Diffuse",ColorRGBA.White);
        mat_default.setFloat("Shininess", 5f); // [1,128]    
        geom.setMaterial(mat_default);
        shootables.attachChild(geom);
        BoundingSphere bs=new BoundingSphere();
        ((Geometry)geom).getMesh().setBound(bs);
        ((Geometry)geom).getMesh().updateBound();
        
        rigidBody = new RigidBodyControl( new HullCollisionShape(((Geometry) geom).getMesh()) , 0.1f ); // dynamic
        geom.addControl(rigidBody);
        rigidBody.setPhysicsLocation(new Vector3f(0f, 6, 0f));
        physicsSpace.add(rigidBody); 
        //drag
        rigidBody.setDamping(0.5f, 0.5f);
        
        
        Health = new Node("hea");
        rootNode.attachChild(Health);
        Geometry g= new Geometry("asd", new Quad(8.4f, 0.4f));
        Material  mat_default3 =  new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
        mat_default3.setColor("Color", ColorRGBA.White); 
        g.setMaterial(mat_default3);
        Health.attachChild(g);
        g.setLocalTranslation(-4.2f, -0.03f, 0);
        Geometry g2= new Geometry("asd", new Quad(8, 0.3f));
        Material  mat_default2 =  new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
        mat_default2.setColor("Color", ColorRGBA.Green); 
        g2.setMaterial(mat_default2);
        Health.attachChild(g2);
        g2.setLocalTranslation(-4, 0, 0.01f);
        Health.addControl(new BillboardControl());
    }
    
    
    @Override
    public void update(float tpf) {
        //update location
        rigidBody.getPhysicsLocation(location);
        rigidBody.getPhysicsRotation(rotation);
        //hot air
        rigidBody.applyCentralForce(Vector3f.UNIT_Y);
        //center of mass
        Vector3f down = new Vector3f(0, -1, 0);
        Vector3f forward = new Vector3f(1, 0, 0);
        Vector3f ndown = new Vector3f(0, -1, 0);
        Vector3f nforward = new Vector3f(1, 0, 0);
        rigidBody.getPhysicsRotation().multLocal(ndown);
        rigidBody.getPhysicsRotation().multLocal(nforward);
        ndown.subtractLocal(down);
        if(ndown.y>0)ndown.y=0;
        nforward.subtractLocal(forward);
        rigidBody.applyForce(ndown, new Vector3f(0, 1, 0));
        rigidBody.applyForce(nforward, new Vector3f(-1, 0, 0));
        //on rails 2,5D, lock Z
        rigidBody.setPhysicsLocation(rigidBody.getPhysicsLocation().multLocal(1, 1, 0));
        //on rails 2,5D, lock max Y
        Vector3f v=rigidBody.getPhysicsLocation();
        if(v.y>20){
            v.setY(20);
            rigidBody.setPhysicsLocation(v);
        }
        
        Health.setLocalTranslation(location.add(0,2.2f,0));
    }

    @Override
    public Vector3f getLocation() {
        return location;
    }
    @Override
    public void setLocation(Vector3f position){
        rigidBody.setPhysicsLocation(position);
    }

    @Override
    public Quaternion getRotation() {
        return rotation;
    }
    
}
